const {ipcRenderer} = require('electron');
const fs = require('fs');
const path = require('path')
const $ = require('./resources/jquery3-7-0.mini')

const todoValue = document.getElementById("todoValue");
const showBox = document.querySelector(".showBox");

window.onload = () => {
    ipcRenderer.send('todo-no-top');
    if(fs.existsSync('./data/saveQuest.json')){
        //有文件
        console.log('文件存在');
    }else{
        //没有文件
        //创建文件夹
        // fs.mkdir('./data',function(err){
        //     console.log('./fs.mkdir error',err);
        // });
        fs.watchFile('./data/saveQuest.json','[]',(error)=>{
            if(error){
                console.log('data/saveQuest.json创建失败',error);
            }
            console.log(`创建成功！`);
        });
        console.log('completed');

    }
}

let i = 0;
const change = () => {
    const pinnedImage = document.getElementById("pinnedImage");
    console.log(pinnedImage.src,i)
    if(i == 0){
        pinnedImage.style.transform = 'rotate3d(0,0,1,-45deg)';
        ipcRenderer.send('todo-top')
        i = 1;
    }else{
        // pinnedImage.src = "images/Pinned_xie.svg"
        pinnedImage.style.transform = 'rotate3d(0,0,1,0deg)';
        ipcRenderer.send('todo-no-top')
        i = 0;
    }
}

const clickClose = async() => {
    ipcRenderer.send('todo-close');
}

let todo_id = 0;
const todoSub = async(event) => {
    // event.preventDefault();
    
    let array =await [];
    if(todoValue.value == ''){
        return
    }
    array.push(todoValue.value);
    let nameBuffer = todoValue.value
    const name1 =await nameBuffer.substr(nameBuffer.indexOf("[") + 1,nameBuffer.indexOf("]")-1)
    const link1 =await nameBuffer.substr(nameBuffer.indexOf("(") + 1,nameBuffer.indexOf(")"))
    console.log(name1)
    console.log(link1)
    showBox.insertAdjacentHTML('afterbegin',`<div style="display:flex;color: #fff;text-shadow: 1px 1px #000;" id="todo_${todo_id}"><p>${todoValue.value}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div class="deleteTODO" onclick="deleteTODO(${todo_id})">删除</div></div>`)
    
    //测试
    // zzbdsFunc()
    // console.log(todoValue.value)
    
    todoValue.value = await '';
    todo_id++;
}

const deleteTODO = async(todo_id) => {
    const showBox = await document.querySelector(".showBox");
    const to_todo_id = document.getElementById(`todo_${todo_id}`)
    to_todo_id.remove()
}

//正则表达式
const zzbdsFunc = async() => {
    const link = /[]/
    // let name1 = todoValue.value[1]
    //从'['到']'
    let nameBuffer = todoValue.value
    const name1 =await nameBuffer.substr(nameBuffer.indexOf("[") + 1,nameBuffer.indexOf("]")-1)
    console.log(name1)
    // console.log(str.search("[]"))
}